/*
 * Copyright 2019, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yyk.yykywd.mvvm

import android.graphics.Typeface
import android.text.InputFilter
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LiveData
import com.google.android.material.textfield.TextInputEditText
import com.yyk.yykywd.R
import com.yyk.yykywd.getCommonTime
import com.yyk.yykywd.mvvm.entity.MeetingBean
import com.yyk.yykywd.mvvm.view_model.BusinessApiStatus
import com.yyk.yykywd.ui.login.LoginViewModel
import com.yyk.yykywd.util.setTextForegroundColor
import com.yyk.yykywd.widght.LoadingLayout
import kotlinx.android.synthetic.main.activity_login.*


@BindingAdapter("businessApiStatus")
fun LoadingLayout.bindStatus(status: BusinessApiStatus?) {
    when (status) {
        BusinessApiStatus.LOADING -> {
            showLoading()
        }
        BusinessApiStatus.ERROR -> {
            showState(resources.getString(R.string.network_error))
        }
        BusinessApiStatus.EMPTY -> {
            showEmpty("暂无数据")
        }
        BusinessApiStatus.DONE -> {
            showContent()
        }
    }
}

@BindingAdapter("startTime")
fun TextView.setStartTime(item: MeetingBean.Data?) {
    item?.let {
        text = getCommonTime(it.start_time)
    }
}

@BindingAdapter("founder")
fun TextView.setFounder(item: MeetingBean.Data?) {
    item?.let {
        text = "创建人：" + item.name
    }
}

@BindingAdapter("createTime")
fun TextView.setCreateTime(item: MeetingBean.Data?) {
    item?.let {
        text = "创建时间：" + getCommonTime(item.create_time)
    }
}

@BindingAdapter("meetingStatus")
fun TextView.setMeetingStatus(item: MeetingBean.Data?) {
    item?.let {
        when (it.status) {
            1 -> {
                text = "会议未开始"
                setTextColor(ContextCompat.getColor(context, R.color.col_5987ff))
            }
            2 -> {
                text = "会议已开始"
                setTextColor(ContextCompat.getColor(context, R.color.col_f5c161))
            }
            3 -> {
                text = "会议已结束"
                setTextColor(ContextCompat.getColor(context, R.color.gray_aa))
            }
        }
    }
}

@BindingAdapter("sendStatus")
fun TextView.setSendStatus(currentTime: Long) {
    if (currentTime != LoginViewModel.DONE) {
        isClickable = false
        text = currentTime.toString() + "s后重发"
        setTextColor(resources.getColor(R.color.gray_c1))
    } else {
        isClickable = true
        text = "获取验证码"
        setTextColor(resources.getColor(R.color.blue_51))
    }
}

@BindingAdapter("forbidden")
fun TextView.setForbidden(forbidden: Boolean) {
    if (forbidden) {
        isClickable = false
        setBackgroundResource(R.drawable.shape_4dp10)
    } else {
        isClickable = true
        setBackgroundResource(R.drawable.shape_4dp9)
    }
}

@BindingAdapter("codeForbidden")
fun TextView.setCodeForbidden(forbidden: Boolean) {
    if (forbidden) {
        isClickable = false
        setTextColor(ContextCompat.getColor(context, R.color.gray_d7))
    } else {
        isClickable = true
        setTextColor(ContextCompat.getColor(context, R.color.blue_43))
    }
}

@BindingAdapter("setIcon")
fun TextInputEditText.setIcon(isPwdLogin: Boolean) {
    if (isPwdLogin) {
        hint = "请输入密码"
        inputType = EditorInfo.TYPE_CLASS_TEXT
        filters = arrayOf()
        setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_mm,0,0,0)
    } else {
        hint = "请输入验证码"
        inputType = EditorInfo.TYPE_CLASS_NUMBER
        filters = arrayOf(InputFilter.LengthFilter(4))
        setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_code,0,0,0)
    }
}

@BindingAdapter("codeStyle1")
fun TextView.setCodeStyle1(isPwdLogin: Boolean) {
    if (isPwdLogin) {
        setBackgroundColor(ContextCompat.getColor(context,R.color.col_ddd))
    } else {
        setBackgroundColor(ContextCompat.getColor(context,R.color.blue_51))
    }
}

